var uploadtemplatepath = GetRootPath() + 'component/commonupload/template.html';
Vue.component('common-upload', function (resolve, reject) {
    $.get(uploadtemplatepath).then(function (res) {
        resolve({
            template: res,
            props: {
                groupguid: {
                    default: ''
                },
                grouptype: {
                    default: ''
                },
                uploadButtonText: {
                    default: '点击上传'
                },
                uploadTipText: {
                    default: ''
                },
                uploadWidth: {
                    default: 360
                },
                readonly: {
                    default: false
                },
                fileTypeExts: {
                    default: '.doc,.docx,.txt,.rar,.zip,.bmp,.jpg,.png,.jpeg,.gif,.pdf,.xls,.xlsx,.ppt,.pptx,.mp4'
                },
                formLabelWidth: {
                    default: '150px'
                },

            },
            data() {
                return {
                    uploadID: getNewGuid(),
                    uploadUrl: apiFrameUrl + "attach/uploadFile",
                    deleteUrl: apiFrameUrl + "attach/deleteFile",
                    fileListUrl: apiFrameUrl + "attach/listByGroupGuid",
                    readAttachUrl: apiFrameUrl + "attach/download?attachGuid=",
                    uploadData: {
                        groupGUid: this.groupguid,
                        groupType: this.grouptype
                    },
                    fileList: [],
                    btnID: getNewGuid(),
                    showErrorInfo: false,
                    errorInfoText: '',
                }
            },
            watch: {
                groupguid: {
                    handler(val) {
                        this.bindFileList();
                    },
                    immediate: true
                }
            },
            mounted() {

            },
            methods: {
                //文件点击事件
                handlePreview(file) {
                    window.top.open(file.url);
                },
                //如果是多选，超过选择文件数量
                handleExceed(files, fileList) {

                },
                //文件上传前验证事件
                beforeUpload(file) {
                    var page = this;
                    if (!this.checkFileExt(file.name, this.fileTypeExts)) {
                        this.errorInfoText = '文件[' + file.name + ']类型不正确,允许的上传文件类型' + this.fileTypeExts;
                        this.showErrorInfo = true;
                        setTimeout(function () {
                            page.showErrorInfo = false
                        }, 4000); //5s后自动隐藏
                        return false;
                    }
                },
                //上传文件成功事件
                handleSuccess(res, file) {
                    this.bindFileList();

                },
                //文件删除前事件
                beforeRemove(file, fileList) {
                    return true;
                },
                //删除文件
                handleRemove(file) {
                    var param = {
                        attachID: file.attachId,
                    }
                    var page = this;
                    JsonAjax(JSON.stringify(param), this.deleteUrl, function (data, status) {
                        if (data.success) {
                            page.bindFileList();
                        }
                    })
                },
                //绑定文件列表
                bindFileList() {
                    var param = {
                        groupGuid: this.groupguid,
                    }
                    var page = this;
                    if (this.groupguid != "") {
                        JsonAjax(JSON.stringify(param), this.fileListUrl, function (data, status) {
                            if (data.success) {
                                data.result.forEach(element => {
                                    element.attguid = element.attachId;
                                    element.url = page.readAttachUrl + element.attachId;
                                    element.name = element.attachName;
                                });
                                Vue.set(page, "fileList", data.result)
                            }

                        })

                    }
                },
                //打开文件
                openFile(file) {
                    window.top.open(file.url);
                },
                getUploadBtnID() {
                    return this.btnID;
                },
                checkIsUpload() {
                    return this.fileList.length > 0;
                },
                //检查附件后缀是否在允许上传范围内
                checkFileExt(filename, fileExt) {
                    var filetype = filename.substring(filename.lastIndexOf("."), filename.length);
                    return fileExt.toLowerCase().indexOf(filetype.toLowerCase()) > -1;
                },
                openVideo(file){
                    window.open(GetRootPath()+"page/attach/attachvideo_play.html?attguid="+file.attguid)
                }
            },

        })
    });
});
